package com.xggstudio.crm.ui.mvp.v;

import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputFilter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.xggstudio.crm.R;
import com.xggstudio.crm.base.BaseActivity;
import com.xggstudio.crm.ui.mvp.p.P_main;

public class MainActivity extends BaseActivity implements IMainView {

    private Fragment[] fragments;

    private int index = 0;

    private BottomNavigationBar bNavigationView;
    private Toolbar toolbar;

    private TextView barText;

    private int fIndex = 0;

    private Menu menu;

    private P_main pMain;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void init(Bundle savedInstanceState) {

        barText = (TextView) $(R.id.bar_text);

        final ClientMangerFragment cmFragment = new ClientMangerFragment();
        MySelfFragment mySelfFragment = new MySelfFragment();

        fragments = new Fragment[]{cmFragment,mySelfFragment};

        getSupportFragmentManager().beginTransaction().add(R.id.content,cmFragment)
                .hide(cmFragment)
                .add(R.id.content,mySelfFragment)
                .hide(mySelfFragment)
                .show(cmFragment)
                .commit();

        toolbar = (Toolbar) $(R.id.toolbar);

        toolbar.setNavigationIcon(R.drawable.search);

        toolbar.setTitleTextColor(Color.WHITE);

        toolbar.setTitle("");

        barText.setText("客户管理");

        barText.setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.bar_botton),null);

        setSupportActionBar(toolbar);

        barText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(index == 0){
                    barText.setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.bar_botton),null);
                    if(fIndex == 0){
                        barText.setText("回访记录");
                        toolbar.setNavigationIcon(null);
                        fIndex = 1;
                    }else{
                        barText.setText("客户管理");
                        toolbar.setNavigationIcon(R.drawable.search);
                        fIndex = 0;
                    }

                    //index = 1;
                }else{
                    barText.setText("个人中心");

                    //index = 0;
                }
                onCreateOptionsMenu(menu);
                cmFragment.toggle(fIndex);

                invalidateOptionsMenu();

            }
        });


        bNavigationView = (BottomNavigationBar) $(R.id.bottom_name);
        bNavigationView.setMode(BottomNavigationBar.MODE_SHIFTING);
        bNavigationView.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);

        bNavigationView.setBackgroundColor(getResources().getColor(R.color.white));

        bNavigationView.setInActiveColor(R.color.line_bg);

        bNavigationView.setActiveColor(R.color.theme_color);

        bNavigationView.addItem(new BottomNavigationItem(R.drawable.home_g,"客户管理"))
                .addItem(new BottomNavigationItem(R.drawable.myself_g,"个人中心"))
                .setFirstSelectedPosition(index > 2 ? 2 : index)
                .initialise();


        bNavigationView.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {

                toggleMain(position);

            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

        pMain = new P_main(this);
        pMain.getAssistList();

    }

    public void reToolbarStyle(int pos){

        switch (pos){
            case 0:
                barText.setText("回访记录");
                toolbar.setNavigationIcon(null);
                fIndex = 1;
                break;
            case 1:
                barText.setText("客户管理");
                toolbar.setNavigationIcon(R.drawable.search);
                fIndex = 0;
                break;
            case 2:

                break;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.menu = menu;
        getMenuInflater().inflate(R.menu.main_bar_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        if(index == 0) {
            if (fIndex == 0) {
                menu.findItem(R.id.menu_add).setVisible(true);

            } else {
                menu.findItem(R.id.menu_add).setVisible(false);
            }
        }else{
            menu.findItem(R.id.menu_add).setVisible(false);
        }

        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.menu_add:
                startToActivity(AddEditCusActivity.class);
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void toggleFragment() {

    }

    @Override
    public void addClient() {

    }

    @SuppressLint("NewApi")
    @Override
    public void toggleMain(int pos) {

        if(index == pos) return;

        FragmentTransaction sfm = getSupportFragmentManager().beginTransaction();
        if(fragments.length > 0){
            if(fragments[pos] != null){
                sfm.hide(fragments[index])
                        .show(fragments[pos])
                        .commit();
            }
        }

        if(pos == 0){
            barText.setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.bar_botton),null);
            if(fIndex == 0){
                barText.setText("客户管理");
                toolbar.setNavigationIcon(R.drawable.search);

            }else{
                barText.setText("回访记录");
                toolbar.setNavigationIcon(null);
            }

        }else{
            barText.setText("个人中心");
            toolbar.setNavigationIcon(null);
            barText.setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,null,null);

        }
        onCreateOptionsMenu(menu);
        invalidateOptionsMenu();

        this.index = pos;
    }
}
